home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcr / pcr4_4.lha / DIST / threads / ThreadsUIOShMem.c < prev    next >
C/C++ Source or Header  |  1990-06-08  |  7KB  |  352 lines

  1. /* begincopyright
  2.   Copyright (c) 1988 Xerox Corporation. All rights reserved.
  3.   Use and copying of this software and preparation of derivative works based
  4.   upon this software are permitted. Any distribution of this software or
  5.   derivative works must comply with all applicable United States export
  6.   control laws. This software is made available AS IS, and Xerox Corporation
  7.   makes no warranty about the software, its performance or its conformity to
  8.   any specification. Any person obtaining a copy of this software is requested
  9.   to send their name and post office or electronic mail address to:
  10.     PCR Coordinator
  11.     Fuji Xerox  IWA  
  12.   endcopyright */
  13.  
  14. /*
  15.  * ThreadsUIOShMem.c
  16.  *
  17.  * Yasuhiko Aizawa December 12, 1989 3:03:15 pm PST
  18.  * Demers, February 12, 1990 3:15:26 pm PST
  19.  * Boehm, June 7, 1990 11:21:23 am PDT
  20.  *
  21.  * Unix SharedMem simulation (for IPC MMap_loader etc),
  22.  *   for Xerox Runtime threads package.
  23.  */
  24.  
  25. #include "xr/Threads.h"
  26. #include "xr/ThreadsBackdoor.h"
  27. #include "xr/ThreadsSharedMem.h"
  28. #include "xr/ThreadsMsgPrivate.h"
  29. #include "xr/UIO.h"
  30. #include "xr/UIOPrivate.h"
  31. #include "xr/Errno.h"
  32.  
  33.  
  34. /*
  35.  * UNIX (SunOS 4.0) dependent memory management ...
  36.  */
  37.  
  38. #include <sys/types.h>
  39. #include <sys/file.h>
  40. #include <sys/ipc.h>
  41. #include <sys/shm.h>
  42.  
  43.  
  44. #define DEBUG_SHM 0
  45.  
  46. /*
  47.  *  ShmAt / ShmDt / ShmGet /ShmCtl
  48.  */
  49.  
  50.  
  51. typedef struct XR_ShmArgsRep {
  52.     int        shm_id;
  53.     XR_Pointer     shm_addr;
  54.     int     shm_flags;
  55.     int        shm_results[XR_MAX_VPS+XR_MAX_IOPS];
  56. } * XR_ShmArgs;
  57.  
  58.  
  59. /* where to put a result ... */
  60.  
  61. #define ResultIndex() ((XR_vpe != NIL) \
  62.             ? XR_vpe->vpe_index \
  63.             : XR_MAX_VPS + XR_iope->iope_index )
  64.  
  65.  
  66.  
  67. /* VP order issuer (runs in IOP) */
  68.  
  69.  
  70. static int /* -errno */
  71. XR_ShmIOPIssueVPOrder (sha, proc)
  72.     XR_ShmArgs sha;
  73.     void (*proc)(/* XR_VPOrder vpo */);
  74. {
  75.     int i, ans;
  76.  
  77.     XR_iope->iope_vpOrderBuf.vpo_sha = ((unsigned)(sha));
  78.     XR_IssueVPOrder(
  79.         /*order*/    &(XR_iope->iope_vpOrderBuf),
  80.         /*proc*/    proc,
  81.         /*stop*/    FALSE
  82.     );
  83.     for( i = 0; i < XR_maxVPs; i++ ) {
  84.         if( (ans = sha->shm_results[i]) != 0 ) return ans;
  85.     }
  86.     return 0;
  87. }
  88.  
  89.  
  90.  
  91. /* shmat primitives */
  92.  
  93. static bool
  94. XR_ValidAddressRangeForShm(addr)
  95.     XR_Pointer addr;
  96. {
  97.     if( addr != XR_ComputeAddress(addr, 0, XR_ROUND_DOWN) ) {
  98.         /* not page-aligned */
  99.         return FALSE;
  100.     }
  101.     return TRUE;
  102. }
  103.  
  104.  
  105. static int /* -errno */
  106. XR_DoShmAtProc (sha)
  107.     XR_ShmArgs sha;
  108. {
  109.     XR_Pointer shmatAns;
  110.     int theResult = 0;
  111.  
  112. #if DEBUG_SHM
  113.     XR_ConsoleMsg("%? DoShmAtProc id  %d addr 0x%x flags 0x%x\n",
  114.         sha->shm_id, sha->shm_addr, sha->shm_flags);
  115. #endif
  116.  
  117.     shmatAns = (XR_Pointer) shmat(
  118.         sha->shm_id, 
  119.     sha->shm_addr, 
  120.     sha->shm_flags);
  121.  
  122.     if( shmatAns != sha->shm_addr) {
  123.         theResult = -errno;
  124.         XR_ConsoleMsg("%? DoShmAtProc shmat ans 0x%x errno %d\n",
  125.             shmatAns, -theResult);
  126.         XR_ConsoleMsg("DoShmAtProc id  %d addr 0x%x flags 0x%x\n",
  127.             sha->shm_id, sha->shm_addr, sha->shm_flags);
  128.     }
  129.     sha->shm_results[ResultIndex()] = theResult;
  130.     return theResult;
  131. }
  132.  
  133.  
  134.  
  135. /* ShmAt VP Procs */
  136.  
  137.  
  138. void
  139. XR_ShmAtVPOrderProc (vpo)
  140.     XR_VPOrder vpo;
  141. {
  142.     XR_ShmArgs sha = (XR_ShmArgs)(vpo->vpo_sha);
  143.  
  144.     if( XR_vpe == NIL )
  145.         XR_Panic("ShmAtVPOrderProc 0");
  146.     (void) XR_DoShmAtProc(sha);
  147. }
  148.  
  149.  
  150. /*  ShmAt IOP Procs */
  151.  
  152.  
  153. void
  154. XR_ShmAtIOPOrderProc (iopo)
  155.     XR_IOPOrder iopo;
  156. {
  157.     int ans;
  158.     XR_ShmArgs sha = ((XR_ShmArgs)(iopo->iopo_sha));
  159.  
  160.     ans = XR_DoShmAtProc(sha);
  161.     if( (ans == 0) && (XR_iope->iope_index == 0) ) {
  162.         ans = XR_ShmIOPIssueVPOrder(sha, XR_ShmAtVPOrderProc);
  163.     }
  164.     iopo->iopo_results[0] = ((unsigned)(ans));
  165. #   if DEBUG_SHM
  166.         XR_ConsoleMsg("%? ShmAtIOPOrderProc done\n");
  167. #   endif
  168.     XR_UIONotifyIOPODone(iopo);
  169. }
  170.  
  171.  
  172. /* ShmAt Exported to UIO.h */
  173.  
  174.  
  175. int
  176. XR_ShmAt(id, addr, flags)
  177.     int  id;
  178.     XR_Pointer addr;
  179.     int flags;
  180. {
  181.     struct XR_ShmArgsRep sha;
  182.     struct XR_IOPOrderRep iopo;
  183.     XR_IOPOResult iopoResult;
  184.     int ans, savedErrno;
  185.  
  186.     if( !XR_ValidAddressRangeForShm(addr) ) {
  187.         XR_SetErrno(EINVAL);
  188.         return(-1);
  189.     }
  190.  
  191.     (void) bzero( &sha, (sizeof sha) );
  192.     sha.shm_id = id;
  193.     sha.shm_addr = addr;
  194.     sha.shm_flags = flags;
  195.  
  196.     iopo.iopo_sha = ((unsigned)(&sha));
  197.  
  198.     iopoResult = XR_IssueIOPOrders(
  199.     /*order*/    &iopo,
  200.     /*remoteProc*/    XR_ShmAtIOPOrderProc,
  201.     /*localProc*/    NIL
  202.     );
  203.     if( iopoResult != XR_IOPO_RESULT_OK ) {
  204.         XR_Panic("ShmAt 0");
  205.     }
  206.     ans = ((int)(iopo.iopo_results[0]));
  207.     if( ans != 0 ) {
  208.         (void) XR_ShmDt(addr);
  209.         XR_SetErrno(-ans);
  210.         return (-1);
  211.     }
  212.     return addr;
  213. }
  214.  
  215.  
  216. /* ShmDt primitives */
  217.  
  218.  
  219. static int /* -errno */
  220. XR_DoShmDtProc(sha)
  221.     XR_ShmArgs sha;
  222. {
  223.     int shmdtAns;
  224.     int theResult = 0;
  225.  
  226. #if DEBUG_SHM
  227.     XR_ConsoleMsg("DoShmDtProc addr %d \n", sha->shm_addr);
  228. #endif
  229.  
  230.     shmdtAns = shmdt( sha->shm_addr );
  231.     if( shmdtAns != 0) {
  232.         theResult = -errno;
  233.         XR_ConsoleMsg("%? DoShmDtProc shmdt errno  addr %d\n",
  234.        -theResult, sha->shm_addr);
  235.     }
  236.     sha->shm_results[ResultIndex()] = theResult;
  237.     return theResult;
  238. }
  239.  
  240.  
  241. /* ShmDt VP Procs */
  242.  
  243.  
  244. void
  245. XR_ShmDtVPOrderProc(vpo)
  246.     XR_VPOrder vpo;
  247. {
  248.     XR_ShmArgs sha = (XR_ShmArgs)(vpo->vpo_sha);
  249.  
  250.     if( XR_vpe == NIL )
  251.         XR_Panic("MUnmapVPOrderProc 0");
  252.     (void) XR_DoShmDtProc(sha);
  253. }
  254.  
  255.  
  256. /* ShmDt IOP Procs */
  257.  
  258.  
  259. void
  260. XR_ShmDtIOPOrderProc (iopo)
  261.     XR_IOPOrder iopo;
  262. {
  263.    int ans, vpAns;
  264.    XR_ShmArgs sha = ((XR_ShmArgs)(iopo->iopo_sha));
  265.  
  266.    ans = XR_DoShmDtProc(sha);
  267.    vpAns = ( (XR_iope->iope_index == 0)
  268.             ? XR_ShmIOPIssueVPOrder(sha, XR_ShmDtVPOrderProc)
  269.             : 0 );
  270.    iopo->iopo_results[0] = ( (unsigned)((ans < 0) ? ans : vpAns) );
  271.    XR_UIONotifyIOPODone(iopo);
  272. }
  273.  
  274.  
  275.  
  276. /* ShmDt Exported to UIO.h */
  277.  
  278.  
  279. int
  280. XR_ShmDt(addr)
  281.     XR_Pointer addr;
  282. {
  283.     struct XR_ShmArgsRep sha;
  284.     struct XR_IOPOrderRep iopo;
  285.     XR_IOPOResult iopoResult;
  286.     int ans;
  287.  
  288.     if( !XR_ValidAddressRangeForShm(addr) ) {
  289.         XR_SetErrno(EINVAL);
  290.         return(-1);
  291.     }
  292.  
  293.     (void)bzero( &sha, (sizeof sha) );
  294.     sha.shm_addr = addr;
  295.     iopo.iopo_sha = ((unsigned)(&sha));
  296.  
  297.     iopoResult = XR_IssueIOPOrders(
  298.     /*order*/    &iopo,
  299.     /*proc*/    XR_ShmDtIOPOrderProc,
  300.        /*localProc*/   NIL
  301.     );
  302.     if( iopoResult != XR_IOPO_RESULT_OK ) {
  303.         XR_Panic("XR_ShMdt 0");
  304.     }
  305.     ans = ((int)(iopo.iopo_results[0]));
  306.     if( ans < 0 ) {
  307.         XR_SetErrno(-ans);
  308.         return (-1);
  309.     }
  310.     return 0;
  311. }
  312.  
  313.  
  314. /* ShmGet exported to UIO.h */
  315.  
  316. int
  317. XR_ShmGet(key, size, shmflg)
  318.     int     key;
  319.     int     size;
  320.     int     shmflg;
  321. {
  322.     return(shmget(key,size,shmflg));
  323. }
  324.  
  325.  
  326. /* ShmCtl exported to UIO.h */
  327.  
  328. int
  329. XR_ShmCtl(shmid, cmd, buf)
  330.     int     shmid;
  331.     int     cmd;
  332.     struct  shmid_ds *buf;
  333. {
  334.     switch(cmd) {
  335.         case IPC_SET:
  336.         case IPC_RMID:
  337.             return(shmctl(shmid,cmd,buf));
  338.         case IPC_STAT:
  339.             {
  340.                 struct shmid_ds myBuf;
  341.                 int ans;
  342.                 
  343.                 ans = shmctl(shmid, IPC_STAT, &myBuf);
  344.                 *buf = myBuf;
  345.                 return(ans);
  346.             }
  347.         default:
  348.             XR_SetErrno(EINVAL);
  349.             return(-1);
  350.     }
  351. }
  352.